package com.googlecode.yoohoo.xmppcore.protocol.stream;

import junit.framework.Assert;

import org.junit.Test;

import com.googlecode.yoohoo.xmppcore.protocol.JabberId;
import com.googlecode.yoohoo.xmppcore.protocol.XmppProtocolException;

public class StreamTest {
	@Test
	public void fromXml() {
		String streamXml =
			"<stream:stream to='example.com' xmlns='jabber:client'" +
			" xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>";
		Stream stream = new Stream();
		
		try {
			stream.fromXml(streamXml);
		} catch (XmppProtocolException e) {
			Assert.fail("Can't parse stream. Shouldn't execute to here");
		}

		Assert.assertEquals(StreamType.CLIENT_TO_SERVER, stream.getStreamType());
		Assert.assertEquals("example.com", stream.getTo().toString());
		Assert.assertEquals("1.0", stream.getVersion());
		
		String invaidStreamXml =
			"<stream:stream to='example.com' xmlns='jabber:client'" +
			" xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>" +
			"</stream>";
		
		stream = new Stream();
		try {
			stream.fromXml(invaidStreamXml);
			Assert.fail("Invalid initial stream. Shouldn't execute to here");
		} catch (XmppProtocolException e) {
		}
	}
	
	@Test
	public void toXml() {
		String streamXml =
			"<stream:stream from='example.com' xmlns='jabber:client'" +
			" xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>";
		Stream stream = new Stream();
		stream.setFrom(JabberId.parseJidString("example.com"));
		
		Assert.assertEquals(streamXml, stream.toXml());
	}
}
